package com.kfc.platform.transaction.service.test;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;


import com.kfc.platform.AppTest;
import com.kfc.platform.transaction.domain.TradeHistory;
import com.kfc.platform.transaction.domain.TradeList;
import com.kfc.platform.transaction.service.TradeHistoryService;
import com.kfc.platform.transaction.service.TransactionService;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:/applicationContext.xml")
@Transactional
public class TransactionServiceTestCase 
{
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String date = df.format(new Date());
	DecimalFormat num = new DecimalFormat("0.000000");
	@Autowired
    private TransactionService transactionservice;
	@Autowired
	private TradeHistoryService tradehistoryService;
    @Test
	@Rollback(false)
	public void testCreateTransaction(){
    	TradeHistory inserthis = new TradeHistory();
    	TradeList tradeList = new TradeList();
    	tradeList.setAmount(new BigDecimal(0.11111).setScale(6, BigDecimal.ROUND_HALF_UP));
    	tradeList.setPrice(new BigDecimal(500));
    	tradeList.setStatus("未成交");
    	tradeList.setTrade_date(date);
    	tradeList.setUsername("aaasss");
    	tradeList.setType("卖");
    	System.out.println("sum:"+new BigDecimal(4).multiply(new BigDecimal(500)));
    	tradeList.setSum(tradeList.getAmount().multiply(tradeList.getPrice()));
    	TradeList temp = transactionservice.insertgetId(tradeList);
    	System.out.println(temp.getListnum());
    	System.out.println(temp.getTrade_date());
    	
    	inserthis.setAmount(temp.getAmount());
    	inserthis.setPrice(temp.getPrice());
    	inserthis.setList_time(temp.getTrade_date());
    	inserthis.setUsername(temp.getUsername());
    	inserthis.setType(temp.getType());
    	inserthis.setStatus(temp.getStatus());
    	inserthis.setListnum(temp.getListnum());
    	inserthis.setSum(temp.getSum());
    	String str2 = tradehistoryService.createTradeHistory(inserthis);
    	System.out.println(str2);
//    	transactionservice.findTransactionbyId(10);
	}
    @Test
    public void TestUpdate(){
    	//假设买了委托单数量的一半
    	TradeList t = transactionservice.findTransactionbyId(3);
    	System.out.println("id:"+t.getListnum());
    	t.setAmount(t.getAmount().subtract(new BigDecimal(2)));//数量-2
    	t.setStatus("部分成交");
    	transactionservice.update(t);
    	TradeHistory th= tradehistoryService.findTradeHistorybylistnum(3, "卖", "未成交");
    	if(th.getAmount().compareTo(new BigDecimal(2)) <= 0)//等于或大于该委托单未成交部分的交易量
    	{
    		th.setStatus("已成交");
    	System.out.println("name:"+th.getUsername());
    		th.setTrade_date(date);
    	}
    	else if(th.getAmount().compareTo(new BigDecimal(2)) == 1)//小于该委托单未成交部分的交易量
    	{//先插入一条新未成交记录，然后更新原纪录数量和状态还有交易时间
    		th.setAmount(th.getAmount().subtract(new BigDecimal(2)));
    		tradehistoryService.createTradeHistory(th);//创建一条新纪录
    		th.setAmount(new BigDecimal(2));
    		th.setStatus("已成交");
    		th.setTrade_date(date);
    	}
    	tradehistoryService.update(th.getId(),th.getAmount(),th.getStatus(),th.getTrade_date());
    }
    @Test
    @Rollback(false)
    public void simpletest(){
    	TradeList t = new TradeList();
    	t.setAmount(new BigDecimal(2));
    	t.setPrice(new BigDecimal(100));
    	t.setStatus("未成交");
    	t.setTrade_date(date);
    	t.setType("买");
    	t.setUsername("kaiser");
    	System.out.println("id:"+transactionservice.savegetid(t));
    }
    @Test
    @Rollback(false)
    public void getbytype(){
    	List<TradeList> ls = transactionservice.getbytype("买");
    	for(TradeList t:ls){
    		System.out.println("委单号："+t.getListnum());
    		System.out.println("用户名："+t.getUsername());
    		System.out.println("价格："+t.getPrice());
    	}
    }

}
